package com.chy.blog_server.modular.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoIndexImage;
import com.chy.blog_server.modular.web.service.IBoIndexImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：首页照片展示表 controller
 * 作者：chenhuiyan
 * 时间：2020/03/08 22:07
 */
@Controller
@RequestMapping(value = "/web/boIndexImage")
public class BoIndexImageController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boIndexImage/";

    @Autowired
    private IBoIndexImageService boIndexImageService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boIndexImage";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boIndexImageService.list(params); //获取数据列表
            Integer count = boIndexImageService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boIndexImage_add";
    }

    /**
     * 保存
     * @param boIndexImage
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> saveData(BoIndexImage boIndexImage, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            if(StrUtil.isEmpty(boIndexImage.getId())){
                boIndexImage.preInsert(request);
                boIndexImageService.save(boIndexImage);
                res.put("state", "200");
                res.put("message", "添加成功");
            }else{
                boIndexImage.preUpdate(request);
                boIndexImageService.updateById(boIndexImage);
                res.put("state", "200");
                res.put("message", "修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }


    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boIndexImageService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 修改是否显示状态
     * @param isShow
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateIsShow")
    @ResponseBody
    public Map<String, Object> updateIsShow(@RequestParam(name = "isShow") Integer isShow,
                                            @RequestParam(name = "indexImageId") String indexImageId, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoIndexImage boIndexImage = boIndexImageService.getById(indexImageId);
            boIndexImage.setIsShow(isShow);
            boIndexImage.preUpdate(request);
            boIndexImageService.updateById(boIndexImage);
            res.put("state", "200");
            res.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "修改失败");
        }
        return res;
    }

    /**
     * 上传照片并生成缩略图
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadIndexImage")
    @ResponseBody
    public Map<String, Object> uploadIndexImage(MultipartFile file){

        //上传
        Map<String, Object> res = UploadFileUtil.fileUploadToOne(file, UploadURLUtil.INDEX_IMAGE, true);

      return res;
    }

    /**
     * 删除已上传的图片和缩略图
     * @param indexImagePath 已上传的照片名
     * @return
     */
    @RequestMapping(value = "/deleteImgLog")
    @ResponseBody
    public Map<String, Object> deleteImgLog(@RequestParam(name="indexImagePath") String indexImagePath, @RequestParam(name="thumUrl")String thumUrl){
        Map<String, Object> res = new HashMap<>();
        String path = UploadURLUtil.INDEX_IMAGE; //路径
        try{
            if(!StrUtil.isEmpty(indexImagePath)){
                String[] urlArr = new String[2];
                urlArr[0] = path + indexImagePath;
                urlArr[1] = path + thumUrl;
                boolean flag = UploadFileUtil.deleteMoreFile(urlArr);
                if(flag){
                    res.put("state", "200");
                    res.put("message", "删除已上传的照片成功");
                }
            }else{
                res.put("state", "200");
                res.put("message", "没有新上传照片");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除已上传的照片失败");
        }
        return res;
    }

    /**
     * 删除
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoIndexImage boIndexImage = boIndexImageService.getById(id);
            boIndexImage.setDelFlag(1);
            boIndexImage.preUpdate(request);
            boIndexImageService.updateById(boIndexImage);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 图片回收站首页
     * @return
     */
    @RequestMapping(value = "/recycleIndex")
    public String recycleIndex(){
        return PREFIX + "boIndexImageRecycle";
    }

    /**
     * 查询已删除的图片列表
     * @return
     */
    @RequestMapping(value = "/recycleList")
    @ResponseBody
    public Map<String, Object> recycleList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boIndexImageService.recycleList(params); //获取数据列表
            Integer count = boIndexImageService.getBaseMapper().selectCount(new QueryWrapper<BoIndexImage>().eq("del_flag", 1)); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 彻底删除（删除原图和缩略图）
     * @param id
     * @return
     */
    @RequestMapping(value = "/thoroughlyDelete")
    @ResponseBody
    public Map<String, Object> thoroughlyDelete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            boIndexImageService.thoroughlyDelete(id);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 恢复相册
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/restore")
    @ResponseBody
    public Map<String, Object> restore(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoIndexImage boIndexImage = boIndexImageService.getById(id);
            boIndexImage.setDelFlag(0);
            boIndexImage.preUpdate(request);
            boIndexImageService.updateById(boIndexImage);
            res.put("state", "200");
            res.put("message", "恢复成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "恢复失败");
        }
        return res;
    }

}
